using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using FiltronASP.Models;

namespace FiltronASP.Areas.Admin.Controllers
{
    /// <summary>
    /// Klasa odpowiedzialna za operacje zwiazane z uwierzytelnianiem uzytkownikow.
    /// </summary>
    [HandleError]
    public class AccountController : Controller
    {

        /// <summary>
        /// Usluga odpowiedzialna za zalogowanie/wylogowanie uzytkownika.
        /// </summary>
        public IFormsAuthenticationService FormsService { get; set; }

        /// <summary>
        /// Usluga odpowiedzialna za walidacje danych uzytkownika.
        /// </summary>
        public IMembershipService MembershipService { get; set; }

        /// <summary>
        /// Metoda inicjujaca kontroler.
        /// </summary>
        /// <param name="requestContext"></param>
        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) 
            { 
                FormsService = new FormsAuthenticationService(); 
            }   //if
            if (MembershipService == null) 
            { 
                MembershipService = new AccountMembershipService(); 
            }   //if
            base.Initialize(requestContext);
        }

        /// <summary>
        /// Domyslna metoda dla linka Admin.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Pages");
            } else {    //if auth
                return RedirectToAction("LogOn");
            }   //else auth
        }

        /// <summary>
        /// Metoda odpowiedzialna za wyswietlanie ekranu do logowania.
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOn()
        {
            return View();
        }

        /// <summary>
        /// Metoda odpowiedzialna za zalogowanie (lub nie) uzytkownika.
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }   //if
                    else
                    {
                        return RedirectToAction("Index", "Pages");
                    }   //else
                }   //if validate user
                else
                {
                    ModelState.AddModelError(String.Empty, String.Empty);
                }   //else validate user
            }   //if valid
            return View(model);
        }

        /// <summary>
        /// Metoda odpowiedzialna za wylogowanie uzytkownika.
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOff()
        {
            FormsService.SignOut();
            return RedirectToAction("LogOn", "Account");
        }

    }
}
